package com.zhihao.takeaway.controller;

import com.zhihao.takeaway.common.utils.DateTool;
import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.common.utils.JwtUtil;
import com.zhihao.takeaway.common.utils.Result;
import com.zhihao.takeaway.entity.*;
import com.zhihao.takeaway.service.DishService;
import com.zhihao.takeaway.service.FlavorService;
import com.zhihao.takeaway.service.MarketerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/Dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private MarketerService marketerService;
    @Autowired
    private FlavorService flavorService;

    @PostMapping("/getFormData")
    public Result getFormData(){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId != null) {
            List<DishFlavorVo> list = dishService.getDishFlavorData(EmployeeId);
            if (list != null) {
                result.success("成功获取数据");
                result.setData(list);
            } else {
                result.fail("未获取到相关信息");
            }
        }else{
            result.fail("当前用户未登录");
        }
        return result;
    }
//    更新菜单
   @PutMapping
    public Result updateDishData(@RequestBody DishFlavorVo dishFlavorVo){
       String EmployeeId = getEmployeeId();
       Result result = new Result();
       if(EmployeeId != null) {
           Dish dish = dishFlavorVo;
           Boolean flag = dishService.updateDish(dish);
           Dish dish1 = dishService.getById(dishFlavorVo.getId());
           Flavor flavor = new Flavor();
           flavor.setName(dishFlavorVo.getFlavorName());
           flavor.setValue(dishFlavorVo.getValue());
           flavor.setId(dish1.getFlavorId());
           flavorService.updateById(flavor);
           if (flag) {
               result.success("更新成功");
           } else {
               result.fail("更新失败，请重新操作");
           }
       }else{
           result.fail("当前用户未登录");
       }
       return result;
   }
   @PostMapping("/deleteDish")
    public Result deleteDishData(@RequestBody Dish dish){
       String EmployeeId = getEmployeeId();
       Result result = new Result();
       if(EmployeeId != null) {
           Boolean flag = dishService.deleteDish(dish);
           if (flag) {
               result.success("删除成功");
           } else {
               result.fail("删除失败");
           }
       }else{
           result.fail("当前用户未登录");
       }
        return result;
   }
   @PostMapping("/sailDish")
    public Result sailDish(@RequestBody Dish dish){
       String EmployeeId = getEmployeeId();
       Result result = new Result();
       if(EmployeeId != null) {
           Boolean flag = dishService.sailDish(dish);
           if (flag) {
               result.success("已成功起售");
           } else {
               result.fail("系统异常，请重新操作");
           }
       }else{
           result.fail("当前用户未登录");
       }
        return result;
   }
   @PostMapping("/stopDish")
    public Result stopDish(@RequestBody Dish dish){
       String EmployeeId = getEmployeeId();
       Result result = new Result();
       if(EmployeeId != null) {
           Boolean flag = dishService.stopDish(dish);
           if (flag) {
               result.success("已成功停售");
           } else {
               result.fail("系统异常，请重新操作");
           }
       }else{
           result.fail("当前用户未登录");
       }
        return result;
   }
//   添加新数据
    @PostMapping("/addDish")
    public Result addDish(@RequestBody DishFlavorVo dishFlavorVo){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId != null) {
            Dish dish = dishFlavorVo;
            dish.setCreateTime(DateTool.getCurrTime());
            dish.setCreateUser(dishFlavorVo.getMarketerId());
            dish.setStatus(1);
            dish.setUpdateTime(DateTool.getCurrTime());
            dish.setUpdateUser(EmployeeId);
            dish.setSort(0);
            dish.setIsDeleted(0);

            Flavor flavor = new Flavor();
            flavor.setName(dishFlavorVo.getFlavorName());
            flavor.setValue(dishFlavorVo.getValue());
            flavor.setIsDeleted(0);
            flavor.setCreateUser(dishFlavorVo.getMarketerId());
            flavor.setCreateTime(DateTool.getCurrTime());
            flavor.setUpdateTime(DateTool.getCurrTime());
            flavor.setUpdateUser(EmployeeId);
            String uid = UUID.randomUUID().toString();
            dish.setFlavorId(uid);
            dishService.save(dish);
            flavor.setId(uid);
            flavorService.save(flavor);
            result.success("已成功添加");

        }else{
            result.fail("当前用户未登录");
        }
        return result;
    }
//    菜单管理中的查询
    @GetMapping("/getMenuData")
    public Result getMenuData(String search){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId != null) {
            List<Dish> list = dishService.getMenuData(search);
            if (list != null && !list.isEmpty()) {
                result.setData(list);
                result.success("成功查询");
            } else {
                result.fail("未查询到相关信息");
            }
        }else{
            result.fail("当前用户未登录");
        }
        return result;
    }
//    用户详情页面操做
    @PostMapping("/getDetailData")
    public Result getDetailData(String id){
        Result result = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录");
        }else{
            Dish dish = dishService.selectById(id);
            result.setData(dish);
            result.success("成功查询");
        }
        return result;
    }
    @PostMapping("/getDishListByMarketerId")
    public Result getDishListByMarketerId(String marketerId){
        Result result = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录");
        }else{
            EasyUITable easyUITable = dishService.getDishListByMarketerId(marketerId);
            Marketer marketer = marketerService.getById(marketerId);
            easyUITable.setEtc(marketer);
            result.setData(easyUITable);

            result.success("成功获取");
        }
        return result;
    }
    public String getEmployeeId(){
        String token = request.getHeader("token");
        String EmployeeId = JwtUtil.validateToken(token);
        return EmployeeId;
    }
    public String getUserId(){
        String token = request.getHeader("token");
        String UserId = JwtUtil.validateToken(token);
        return UserId;
    }
}
